package com.tencent;

import com.tencent.utils.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class esTest2 {

    @Test
    public void testAdd(){
        for (int i = 1; i < 50; i++) {
            // 获取链接
            TransportClient client = ESClientUtil.getClient();
            // 设置上下文对象
            IndexRequestBuilder builder = client.prepareIndex("pethome", "employee", "" + i);
            // 新增一个map 装employee数据
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i);
            map.put("name", i % 2 == 0 ? "张春勇" : "徐园");
            map.put("age", i);
            map.put("sex", i % 2);
            // 将map放进source里
            IndexRequestBuilder indexRequestBuilder = builder.setSource(map);
            IndexResponse indexResponse = indexRequestBuilder.get();
            System.out.println(indexResponse);
        }
    }


    @Test
    public void testGet(){
        TransportClient client = ESClientUtil.getClient();
        GetRequestBuilder builder = client.prepareGet("pethome", "employee", "1");
        // 打印整个上下文对象
        System.out.println(builder.get());
        // 只打印source中的参数
        System.out.println(builder.get().getSource());
    }

    @Test
    public void testUpdate(){
        TransportClient client = ESClientUtil.getClient();
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "employee", "1");
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "徐园");
        // 将修改的方静doc里
        UpdateRequestBuilder builder = updateRequestBuilder.setDoc(map);
        UpdateResponse updateResponse = builder.setDoc(map).get();
        System.out.println(updateResponse);
    }

    @Test
    public void testDelete(){
        TransportClient client = ESClientUtil.getClient();
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "employee", "1");
        // 删除第一列
        DeleteResponse deleteResponse = deleteRequestBuilder.get();
        System.out.println(deleteResponse);
    }

    @Test
    public void test(){
        TransportClient client = ESClientUtil.getClient();
        // 设置库
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        // 设置type
        builder.setTypes("employee");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        // 查询
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", "徐园"));
        // 过滤
        boolQueryBuilder.filter(QueryBuilders.termQuery("sex", 1));
        boolQueryBuilder.filter(QueryBuilders.rangeQuery("age").gte(10).lte(40));
        builder.setQuery(boolQueryBuilder);
        // 设置从第一页开始
        builder.setFrom(1);
        // 设置每页展示5条
        builder.setSize(5);
        // 按照年龄倒叙排列
        builder.addSort("age", SortOrder.DESC);
        // 执行得到结果
        SearchResponse searchResponse = builder.get();
        SearchHits hits = searchResponse.getHits();
        SearchHit[] hitss = hits.getHits();
        for (SearchHit documentFields : hitss) {
            System.out.println(documentFields.getSourceAsMap());
        }
    }
}
